package com.asktgapp.engineer.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.engineer.activity.AccountRecordActivity;
import com.asktgapp.engineer.activity.ApplyForMoenyActivity;
import com.asktgapp.engineer.activity.IDVerificationActivity;
import com.asktgapp.model.BalanceVO;
import com.asktgapp.modulebase.common.util.PreferencesUtil;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * time:2018/1/22
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description: 余额fragment
 */

public class BalanceFragment extends BaseFragment {
    @InjectView(R.id.tv_balance_balance)
    TextView mBalance;
    @InjectView(R.id.tv_balance_withdraw)
    TextView mWithDraw;

    private BalanceVO bean;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_balance, container, false);
        ButterKnife.inject(this, viewGroup);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        setHasOptionsMenu(true);
        mWithDraw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(bean!=null) {
                    if (bean.getHasTixianPwd() == 0) {
                        startActivity(new Intent(getActivity(), IDVerificationActivity.class));
                    } else {
                        Intent intent = new Intent(getActivity(), ApplyForMoenyActivity.class);
                        intent.putExtra("balance", String.valueOf(bean.getMoney()));
                        startActivity(intent);
                    }
                }
            }
        });
    }

    @Override
    public void loadData() {
        visibleLoading();
        Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
        Call<ApiResponseBody<BalanceVO>> call = apiservice.getProjecterMoney(params);
        call.enqueue(new Callback<ApiResponseBody<BalanceVO>>() {
            @Override
            public void onResponse(Call<ApiResponseBody<BalanceVO>> call, Response<ApiResponseBody<BalanceVO>> response) {
                inVisibleLoading();
                if (response.isSuccessful()) {
                    bean = response.body().getResult();
                    mBalance.setText("¥ " +String.valueOf( bean.getMoney()));
                } else {
                    showTost(response.raw().message(), 1);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody<BalanceVO>> call, Throwable t) {
                inVisibleLoading();
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(),0);
                }
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.detail, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        startActivity(new Intent(getActivity(), AccountRecordActivity.class));
        return super.onOptionsItemSelected(item);
    }
}
